//
//  KLScreenVideoCell2.m
//  kularkDemo
//
//  Created by szblsx2 on 2020/4/9.
//  Copyright © 2020 szblsx. All rights reserved.
//

#import "KLScreenVideoCell2.h"
#import "KLRemoteVideoCell.h"

@interface KLScreenVideoCell2 ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) BOOL focus;
@property (nonatomic, assign) NSInteger superIndex;

@end

@implementation KLScreenVideoCell2

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupViewsFrame:frame];
    }
    return self;
}

- (void)setupViewsFrame:(CGRect)frame
{
    UICollectionView *videoRenderer = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    videoRenderer.scrollEnabled = NO;
    [videoRenderer registerClass:[KLRemoteVideoCell class] forCellWithReuseIdentifier:kRemoteVideoCellIdentifier];
    videoRenderer.dataSource = self;
    videoRenderer.delegate = self;
    [self addSubview:videoRenderer];
    
    _collectionView = videoRenderer;
}

- (void)reloadCellWithRemoteVideoTracks:(NSArray *)dataArray focus:(BOOL)focus atSuperIndex:(NSInteger)superIndex
{
    _superIndex = superIndex;
    _focus = focus;
    _dataArray = dataArray;
    
    [self reloadVideoRender];
}

- (void)reloadVideoRender{
    __weak __typeof(self) ws = self;
    [UIView animateWithDuration:0 animations:^{
        [ws.collectionView reloadData];
    }];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArray.count / 4 >= self.superIndex+1) {
        return 4;
    }else{
        return self.dataArray.count%4;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLRemoteVideoCell *remoteCell = [collectionView dequeueReusableCellWithReuseIdentifier:kRemoteVideoCellIdentifier forIndexPath:indexPath];
    
    KLMeetUser *userModel = self.dataArray[self.superIndex * 4 + indexPath.row];
    [remoteCell reloadCellWithRemoteUser:userModel focus:self.focus];
    return remoteCell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cWidth = collectionView.frame.size.width;
    CGFloat cHeight = collectionView.frame.size.height;
    return CGSizeMake(cWidth/2, cHeight/2);
}

#pragma mark - Getter

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

@end
